package shellstation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import net.sourceforge.jcalendarbutton.JCalendarButton;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.table.DefaultTableModel;
import java.sql.ResultSet;


public class InventoryReportView implements ActionListener{


    private    JLabel shellProduct;
 
    private    JButton goIconButton;
    
    private DefaultTableModel inventory;

    private    JTable shellProductTable;
    private    String[] shellpCol = {"Product","Opening Inventory", "Receipts","Adjustments", "Total","Closing Inventory","Sales", "Price", "Sales Amount"};
    
    private   JFrame frame;
    private   JPanel panel;

    private net.sourceforge.jcalendarbutton.JCalendarButton  jCalendar;
    private DateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
    private JTextField dateField;

    private Integer[] monthNum = new Integer[13];
    private Integer[] dayNum = new Integer[32];

    private Icon goIcon;

    public void InventoryReportView() {

        frame = new JFrame("Inventory Report");
        frame.setSize(1000,650);
        frame.setVisible(true);
        frame.setResizable(false);

        jCalendar= new JCalendarButton();
        jCalendar.setBounds(20, 52, 25, 25);
        jCalendar.addPropertyChangeListener(new java.beans.PropertyChangeListener(){

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                if (evt.getNewValue() instanceof Date)
                    setDate((Date)evt.getNewValue());
            }
        });

        dateField = new JTextField("", 10);
        dateField.setBounds(50, 55, 138, 20);
        dateField.setEditable(false);
        dateField.addFocusListener(new java.awt.event.FocusAdapter(){

            public void focusLost(java.awt.event.FocusEvent evt) {
            	String date = dateField.getText();
                setDate(date);
            }

        });
               

        goIcon = new ImageIcon(getClass().getResource("image/search.gif"));

        goIconButton = new JButton();
        goIconButton.setIcon(goIcon);
        goIconButton.setBounds(210,52,25,25);
        goIconButton.setBackground(null);
	goIconButton.addActionListener(this);

        shellProduct = new JLabel("Shell Inventory");
	shellProduct.setFont(new Font("Hobo Std", Font.BOLD, 20));
        shellProduct.setBounds(450,-230,500,500);

        inventory = new DefaultTableModel(35,9);
        inventory.setColumnIdentifiers(shellpCol);

        shellProductTable = new JTable(inventory){
            public boolean isCellEditable(int shellpRow, int shellpCol){
                return false;
            }};
        shellProductTable.setPreferredScrollableViewportSize(new Dimension(800,150));
        shellProductTable.setFillsViewportHeight(true);
               


        JScrollPane scrollPane3 = new JScrollPane(shellProductTable);
        scrollPane3.setBounds(20, 100, 950, 500);
        panel = new JPanel(null);
                
        panel.add(goIconButton);
        panel.add(shellProduct);
        panel.add(scrollPane3);
        panel.add(jCalendar);
        panel.add(dateField);

        frame.add(panel);
   }


   public JLabel getShellProduct(){
       return shellProduct;
   }

   public JTable getShellProductTable(){
       return shellProductTable;

   }
   
   public void setDate(Date date)
   {
        String dateString = "";
        if (date != null)
            dateString = dateFormat.format(date);
            dateField.setText(dateString);
            jCalendar.setTargetDate(date);
   }

   public void setDate(String dateString)
   {
		Date date = null;
		try	{
                    if ((dateString != null) && (dateString.length() > 0))
                    date = dateFormat.parse(dateString);
		}
                catch (Exception e)	{
                    date = null;
		}


                this.setDate(date);
   }
   


  public void actionPerformed(ActionEvent e){

        if(e.getSource() == goIconButton){
             ResultSet rs, rs2, rs3, rs4;
             InventoryReportModel irm= new InventoryReportModel();

             rs= irm.getProduct();
             int i=0;

             try{
                while(rs.next()){
                    shellProductTable.setValueAt(rs.getString("product"), i, 0);
                    shellProductTable.getColumnModel().getColumn(0).setPreferredWidth(115);
                    i++;
					}
				}

             catch(Exception ex){
                ex.printStackTrace();
				}	

            rs= irm.getQStock(dateField.getText());
            rs2= irm.getQReceipt(dateField.getText());
            rs3 = irm.getMaxProduct();
            int j;
            int[] stock1;
            int[] receipt1;
            int[] opening;
             
            try{
				rs3.next();          
				j = rs3.getInt("ID");
				stock1 = new int[j]; 
				receipt1 = new int[j];
				opening = new int[j];
             
				rs.next();
				rs2.next();
				for(int k = 0; k< j ; k++){        
					if(k+1 == rs.getInt("ID") &&  rs.getObject("stock") != null){
                        stock1[k] = rs.getInt("stock");
						rs.next();
						}
                 
					else{
						stock1[k] = 0;
						}
                 
                 	if(k + 1 == rs2.getInt("ID") && rs2.getObject("receipt") != null){
                        receipt1[k] = rs2.getInt("receipt");
						rs2.next();
					}
                 
					else{
						receipt1[k] = 0;
						}
                 
                 	opening[k] = stock1[k] - receipt1[k];
                 	shellProductTable.setValueAt(opening[k], k, 1);
					}
				}
        
            catch(Exception ex){
                 ex.printStackTrace();
				}
             
			rs = irm.getReceipts(dateField.getText());
			rs3 = irm.getMaxProduct();
			j = 0;
			int[] receipt2;
           
			try{
				rs.next();
				rs3.next();
				j = rs3.getInt("ID");
				receipt2 = new int[j];

                for(i = 0; i< j ; i++){
					if(i + 1 == rs.getInt("ID") && rs.getObject("receipt") != null){
						receipt2[i] = rs.getInt("receipt");
                        rs.next();
						}
					else
                       receipt2[i] = 0;
					shellProductTable.setValueAt(receipt2[i], i, 2);
					shellProductTable.setValueAt(receipt2[i], i, 6);
                    }
				}
			
			catch(Exception ex){
                ex.printStackTrace();
				}
           
            rs = irm.getAdjustments(dateField.getText());
            rs3 = irm.getMaxProduct();
            j=0;
            int[] adjustments;
            
            try{
                rs.next();
                rs3.next();
                j = rs3.getInt("ID");
                adjustments = new int[j];
                 
                for(i = 0; i<j; i++){
                    if(i+1 == rs.getInt("ID") && rs.getObject("adjustments")!= null){
                        adjustments[i] = rs.getInt("adjustments");
                        rs.next();
						}
                    else
                        adjustments[i] = 0;
                    shellProductTable.setValueAt(adjustments[i], i, 3);
					}
				}
             
            catch(Exception ex){
                ex.printStackTrace();
				}
             
            int total;
            i=0;
            rs3 = irm.getMaxProduct();
            j = 0;
             
            try{
                try{
                    rs3.next();
                    j = rs3.getInt("ID");
					}
                catch(Exception ex){
                     ex.printStackTrace();
					}
                 
                while( i < j){
                    String open = shellProductTable.getValueAt(i, 1).toString();
                    String adj = shellProductTable.getValueAt(i, 3).toString();
                    String rec = shellProductTable.getValueAt(i, 2).toString();

                    int op=0;
                    int ad=0;
                    int re=0;

                    try{
                        op = Integer.parseInt(open);
                        ad = Integer.parseInt(adj);
                        re = Integer.parseInt(rec);
						}

                    catch (NumberFormatException nfe){
                        throw(nfe);
						}

                    total = (op + ad) - re;

                    shellProductTable.setValueAt(total, i, 4);
                    shellProductTable.setValueAt(total, i, 5);
                    i++;
					}
				}

            catch(NullPointerException np){
                throw(np);
				}
             
            rs = irm.getPrice(dateField.getText());
            rs3 = irm.getMaxProduct();
            j = 0;
            String[] price1;
             
            try{
                rs.next();
                rs3.next();
                j = rs3.getInt("ID");
                price1 = new String[j];        
             
                for(i = 0; i < j ; i++){
					if(i + 1 == rs.getInt("ID")){
						price1[i] = rs.getString("price");
						rs.next();
						}  
					else{
						price1[i] = "P0.00";     
						}
					shellProductTable.setValueAt(price1[i], i, 7);  
					}
                }
                      
            catch(Exception ex){
                ex.printStackTrace();
				}

            rs= irm.getAmount(dateField.getText());
            rs3 = irm.getMaxProduct();
            j = 0;
            String[] amount;
            
			try{
                rs.next();
                rs3.next();
                j = rs3.getInt("ID");
                amount = new String[j];
                
                for( i = 0; i< j; i++){
                    if(i+1 == rs.getInt("ID")){
                        amount[i] = rs.getString("amount");
                        rs.next();
						}
                    else{
                        amount[i] = "P0.00";
						}
                    
                    shellProductTable.setValueAt(amount[i], i, 8);
					}
				}
            catch(Exception ex){
				ex.printStackTrace();
				}
          
			}	
	}



  public static void  main(String[] args){
      InventoryReportView i = new InventoryReportView();
      i.InventoryReportView();
  }

}

  